﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections.ObjectModel;
using System.IO;
using DocumentsTagger.DM;

namespace DocumentsTagger.UI.Model
{
    /// <summary>
    /// This class is a Model for UI puporses that will hold the UI part that shows the folders in the 
    /// selection pane.
    /// </summary>
    public class FolderUIModel : DependencyObject
    {
        #region members
        private readonly Dictionary<string, DocumentUIModel> r_DocumentsDict = new Dictionary<string, DocumentUIModel>();
        #endregion

        #region properties
        /// <summary>
        /// gets or sets the folder name (the last part of its path)
        /// </summary>
        public String FolderName { get; set; }

        /// <summary>
        /// gets or sets the folder full path 
        /// </summary>
        public String FolderPath { get; set; }

        /// <summary>
        /// holds the documents under that folder
        /// </summary>
        public ObservableCollection<DocumentUIModel> Documents { get; set; }
        #endregion

        #region ctor
        /// <summary>
        /// creates the folder ui model
        /// </summary>
        /// <param name="FolderPath">the full path of the folder</param>
        public FolderUIModel(string FolderPath)
        {
            this.FolderPath = FolderPath;
            this.FolderName = Path.GetFileName(FolderPath);
            this.Documents = new ObservableCollection<DocumentUIModel>();
        }
        #endregion

        #region methods
        /// <summary>
        /// checks if folder contains a specific documents
        /// </summary>
        /// <param name="FullPath">full path of the file</param>
        /// <returns>true if the folder contains the file, false otherwise</returns>
        public bool HasDocument(string FullPath)
        {
            return r_DocumentsDict.ContainsKey(FullPath);
        }

        /// <summary>
        /// Adds a document to the folder.
        /// </summary>
        /// <param name="doc">the document to add.</param>
        public void AddDocument(Document doc)
        {
            string fullPath = doc.FullPath;
            int i = 0;
            if (!r_DocumentsDict.ContainsKey(fullPath))
            {
                DocumentUIModel docVM = new DocumentUIModel(doc);

                r_DocumentsDict.Add(fullPath, docVM);
                while ((i < Documents.Count) && (Documents[i].FileName.CompareTo(doc.FileName) == -1))
                {
                    i++;
                }
                Documents.Insert(i,docVM);
            }
        }

        /// <summary>
        /// remvoes a document from the folder.
        /// </summary>
        /// <param name="FullPath"></param>
        public void RemoveDocument(string FullPath)
        {
            if (r_DocumentsDict.ContainsKey(FullPath))
            {
                DocumentUIModel docVM = r_DocumentsDict[FullPath];

                r_DocumentsDict.Remove(FullPath);
                Documents.Remove(docVM);
            }
        }
        #endregion
    }
}
